package com.clkj.zhonghui.dao.stored;

import com.clkj.zhonghui.pojo.stored.UserStored;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * @author yawang
 * @create 2024/07/12 11:32:00
 **/
@Repository
public interface UserStoredMapper extends Mapper<UserStored> {

    @Update("if exists (select * from t_zh_userStored where USER_ID = #{userId}) update t_zh_userStored set share_money +=#{money} where user_id = #{userId} " +
            "else insert into t_zh_UserStored values (#{userId},0,0,#{money},#{shopId},0) ")
    void updateParentStored(Integer userId,Double money,Integer shopId);

    @Update("update t_zh_UserStored set total_stored +=#{storedNum},residue_money +=#{storedNum},vip_grade = #{grade} where user_id = #{userId}")
    void updateUserStore(Integer userId,Double storedNum,Integer grade);

    @Update("update t_zh_UserStored set residue_money -=#{storedNum} where user_id = #{userId}")
    void updatePayUser(Integer userId,Double storedNum);

    @Select("<script>" +
            "select a.*,b.user_name from t_cl_userStored a left join t_cl_user b on a.user_id = b.user_id " +
            "where a.total_money >0 " +
            "<if test='userId!=0'> and a.user_id = #{userId} </if>" +
            "order by stored_id desc" +
            "</script>")
    List<UserStored> listAllUserStored(@Param("userId")Integer userId);


}
